<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
            $geohash = new \Org\Util\geohash();
            
            //经纬度转换成Geohash
            $test=D("test");
            //获取附近的信息
            $n_latitude = $_GET['la'];
            $n_longitude = $_GET['lo'];


            //方案B geohash求出附近，然后排序
            //当前 geohash值
            $n_geohash = $geohash->encode($n_latitude,$n_longitude);
            //附近，参数n代表Geohash，精确的位数，也就是大概距离；n=6时候，大概为附近1千米
            $n = $_GET['n'];
            $like_geohash = substr($n_geohash, 0, $n);
            $sql = 'select * from tb_test where geohash like "'.$like_geohash.'%"';
            $data = M()->query($sql);

            //算出实际距离
            foreach($data as $key =>$val) {
                $distance =$this-> getDistance($n_latitude, $n_longitude, $val['lat'], $val['lng']);
                $data[$key]['distance'] = $distance;
                //排序列
                $sortdistance[$key] = $distance;
            }
            //距离排序
            array_multisort($sortdistance,SORT_ASC,$data);
            echo $data[0]['distance'];
            dump($data);


    }
    //根据经纬度计算距离 其中A($lat1,$lng1)、B($lat2,$lng2)
    public function getDistance($lat1, $lng1, $lat2, $lng2) {
        //地球半径
        $R = 6378137;
        //将角度转为狐度
        $radLat1 = deg2rad($lat1);
        $radLat2 = deg2rad($lat2);
        $radLng1 = deg2rad($lng1);
        $radLng2 = deg2rad($lng2);
        //结果
        $s = acos(cos($radLat1)*cos($radLat2)*cos($radLng1-$radLng2)+sin($radLat1)*sin($radLat2))*$R;
        //精度
        $s = round($s* 10000)/10000;
        return  round($s);
    }
    //type1为患者版2位医生版
    public function BBSqrcode($str){
        vendor('phpqrcode.phpqrcode');
        //定义纠错级别
        $errorLevel = "L";
        //定义生成图片宽度和高度;默认为3
        $size = "4";
        //定义生成内容
        $content=$str;
        //调用QRcode类的静态方法png生成二维码图片//
        $QRcode = new \QRcode();
        $QRcode::png($content, false, $errorLevel, $size);
        //生成网址类型
//        $url.="http://ivf-bb.com/tb/index.php?m=Home&c=Index&a=get_user_qrcode&type=".$type."&no=".$no;
//        $QRcode::png($url, false, $errorLevel, $size);
    }
    
    
    
    //type1为患者版2位医生版
    public function qrcode($type,$no){
        vendor('phpqrcode.phpqrcode');
        //定义纠错级别
        $errorLevel = "L";
        //定义生成图片宽度和高度;默认为3
        $size = "4";
        //定义生成内容
//        $content='';
        //调用QRcode类的静态方法png生成二维码图片//
        $QRcode = new \QRcode();
//        $QRcode::png($content, false, $errorLevel, $size);
        //生成网址类型
        $url.="http://ivf-bb.com/tb/index.php?m=Home&c=Index&a=get_user_qrcode&type=".$type."&no=".$no;
        $QRcode::png($url, false, $errorLevel, $size);
    }
    public function get_user_qrcode(){
        $no=I("get.no");
        $type=I("get.type");
        $ip=$this->getIP();
        $create_time=time();
        $test=D("Test");

        $test->add(array("ips"=>$ip,"qrcode"=>$no,"s_time"=>time()));

        if($type==1){
            header("Location:https://itunes.apple.com/en/app/id1063969912");
        }else{
            header("Location:https://itunes.apple.com/en/app/id1063640465");
        }
        
    }
/**
     * //获取客户端ip地址
     * @return string
     */
    private function getIP()
    {
        if (getenv("HTTP_CLIENT_IP"))
        {
            $ip = getenv("HTTP_CLIENT_IP");
        }
        else
        {
            if (getenv("HTTP_X_FORWARDED_FOR"))
            {
                $ip = getenv("HTTP_X_FORWARDED_FOR");
            }
            else
            {
                if (getenv("REMOTE_ADDR"))
                {
                    $ip = getenv("REMOTE_ADDR");
                }
                else
                {
                    $ip = "Unknow";
                }
            }
        }

        return $ip;
    }

    //极光推送
    public function Jpush(){
        $fetion = new \Org\Util\JpushSend();
         $receive = 'all';//全部
          //$receive = array('tag'=>array('123'));//标签
            //$receive = array('alias'=>array('1'),);//别名
            $content = '测试测试';
            $m_type = 'ooxxqqqqqq';
            $m_txt = '123456789';
            $m_time = '600';        //离线保留时间
            $res=$fetion->send_pub($receive, $content ,$m_type, $m_txt ,$m_time); 
    }
    //爬虫
    public function py(){
        
        $cv="http://www.lifeivfcenter.com/";
        $nw=urlencode($cv);
        $ch=curl_init();
        curl_setopt($ch,CURLOPT_URL,$cv);
        curl_setopt($ch,CURLOPT_HEADER,0);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        $output=curl_exec($ch);
        curl_close($ch);
        echo $output;
    }
    public function swo(){
        $http = new \swoole_http_server("0.0.0.0", 9501);

        $http->on('request', function ($request, $response) {
            var_dump($request->get, $request->post);
            $response->header("Content-Type", "text/html; charset=utf-8");
            $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
        });

        $http->start();
    }

}